<?php

namespace App\V3\V3_2_6\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class AdminModel extends Model
{


    use HasFactory;

    protected $table = 'admin';
    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储

    //toArray 时间转换问题
    public static function getAccountInfo(int $userId)
    {
        return self::where('id', $userId)->first();
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    //获取审批人列表
    static public function getApproverList($parentId)
    {
        return self::select(
            'id',
            'username',
            'nickname',
            'mobile',
            'parent_id'
        )
            ->where('parent_id', $parentId)
            ->where('delete_flag', 0)
            ->orWhere('id', $parentId)
            ->orderBy('id', 'ASC')
            ->get();
    }

    //用户列表
    static public function userList($parentId, $data)
    {
        $result = self::select(
            'id',
            'username',
            'nickname',
            'mobile',
            'is_active',
            'newpwd',
            'created_at',
            'updated_at'
        )
            ->where([
                'parent_id' => $parentId,
                'delete_flag' => 0
            ])
            ->where(function($query) use ($data) {
                $query->orWhere('nickname', 'like', '%'.$data['keyword'].'%');
                $query->orWhere('username', 'like', '%'.$data['keyword'].'%');
            })
            ->orderBy('id', 'DESC')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);

        return $result;
    }

    //变更手机号
    public static function changeMobile($userId, $data)
    {
        return self::where('id', $userId)->update([
            'mobile' => $data['mobile'],
            'username' => $data['mobile'],
        ]);
    }
}
